/*
 * my_lib.c
 *
 *  Created on: Nov 26, 2012
 *      Author: andrey
 */

#include "my_lib.h"

static __IO uint32_t CurrentTime = 0;

int GetCurrentTime() {
	return CurrentTime;
}

// this method is invoked by system tick interrupt
void TimingDelay_Decrement(void) {
	CurrentTime++;
}

void Delay(__IO uint32_t msDelay) {
	// save number of needed ticks
	int expectedTimingDelay = CurrentTime + 1 * msDelay;
	// wait for the ticks to happen (see TimingDelay_Decrement)
	while (CurrentTime < expectedTimingDelay)
		;
}


void mySTM_EVAL_LEDInit(uint32_t RCC_AHB1Periph, uint32_t pin,
		GPIO_TypeDef* GPIOx) {
	GPIO_InitTypeDef GPIO_InitStructure;

	/* Enable the GPIO_LED Clock */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph, ENABLE);

	/* Configure the GPIO_LED pin */
	GPIO_InitStructure.GPIO_Pin = pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOx, &GPIO_InitStructure);
}

void turnPinOff(GPIO_TypeDef* GPIOx, uint32_t pinBits) {
	GPIOx ->BSRRH = pinBits;
}

void turnPinOn(GPIO_TypeDef* GPIOx, uint32_t pinBits) {
	GPIOx ->BSRRL = pinBits;
}
